local LUF = LibStub("AceAddon-3.0"):GetAddon("lUnitFrame")
local db
local options 


local MODNAME = "ClassIcon"
local ClassIcon = LUF:NewModule(MODNAME, "AceEvent-3.0")



local function getOptions()
	if not options then
		options ={
			type = "group",
			name = "ClassIcon",
			arg = MODNAME,
			args = {
				genhead = {
					type = "header",
					name = "ClassIconSet",
					order = 10,
				},
				player = {
					type = "toggle",
					name = "Player",
					order = 20,
					width = "double",
					get = function() return db["player"].enable end,
					set = function(info,v) db["player"].enable = v
						ClassIcon:UpdateClassIcon("player")
					end,
				},
				playerx = {
					type = "input",
					name = "x",
					order = 25,
					
					get = function() return tostring(db["player"].x) end,
					set = function(info,v) 
						db["player"].x = v
						ClassIcon:MoveIcon("player") 
					end,
						
				},
				playery = {
					type = "input",
					name = "y",
					order=26,
					
					get = function() return tostring(db["player"].y) end,
					set = function(info,v) 
						db["player"].y = v 
						ClassIcon:MoveIcon("player") 
					end,
				},
				target = {
					type = "toggle",
					name = "target",
					order = 30, 
					width = "double",
					get = function() return db["target"].enable end,
					set = function(info,v) db["target"].enable =v 
						ClassIcon:UpdateClassIcon("target")
					end,
				},
				targetx = {
					type = "input",
					name = "x",
					order = 31,
					
					get = function() return tostring(db["target"].x) end,
					set = function(info,v) 
						db["target"].x = v 
						ClassIcon:MoveIcon("target") 
					end,
				},
				targety = {
					type = "input",
					name = "y",
					order=32,
					
					get = function() return tostring(db["target"].y) end,
					set = function(info,v) 
						db["target"].y =v
						LUF:Print(db["target"].y..":"..v)
						ClassIcon:MoveIcon("target") 
					end,
				},
				party1 = {
					type = "toggle",
					name = "party1",
					order = 40, 
					width = "double",
					get = function() return db["party1"].enable end,
					set = function(info,v) db["party1"].enable =v 
						ClassIcon:UpdateClassIcon("party1")
					end,
				},
				party1x = {
					type = "input",
					name = "x",
					order = 41,
					
					get = function() return tostring(db["party1"].x) end,
					set = function(info,v) 
						db["party1"].x = v 
						ClassIcon:MoveIcon("party1") 
					end,
				},
				party1y = {
					type = "input",
					name = "y",
					order=42,
					
					get = function() return tostring(db["party1"].y) end,
					set = function(info,v) 
						db["party1"].y =v
						LUF:Print(db["party1"].y..":"..v)
						ClassIcon:MoveIcon("party1") 
					end,
				},
					
			},
		}
	end
	return options
end

function ClassIcon:OnInitialize()
	self.db = LUF.db:RegisterNamespace(MODNAME)
    self.db:RegisterDefaults({
        profile = {
			["player"] = { x =75, y =0,barderx=11,bardery = -12 ,enable = true};
			["target"] = { x =109, y =2,barderx=11,bardery = -12, enable = true};
			["party1"] = { x =75, y =0,barderx=11,bardery = -12 ,enable = true};
        },
    })
	db = self.db.profile
	LUF:RegisterModuleOptions(MODNAME, getOptions)
	

	self:CreateIcon("player");
	self:MoveIcon("player");
	self:CreateIcon("target");
	self:MoveIcon("target");
	self:CreateIcon("party1");
	self:MoveIcon("party1");
	
end

function ClassIcon:PLAYER_TARGET_CHANGED()
	self:UpdateClassIcon("target")
end

function ClassIcon:MoveIcon(unit)
--if v then 
--DEFAULT_CHAT_FRAME:AddMessage(db[unit].x..":"..db[unit].y..":"..v)
--end
--if not unit then return end
--if x then db[unit].x = tonumber(x) end
--if y then db[unit].y = tonumber(y) end
if LUF.frames[unit].lufclassicon then
	LUF.frames[unit].lufclassicon:ClearAllPoints();
	LUF.frames[unit].lufclassicon:SetPoint("TOPLEFT",LUF.frames[unit],"TOPLEFT",db[unit].x,db[unit].y)
end
end


function ClassIcon:ToggleClassIcon(unit,temp)
--DEFAULT_CHAT_FRAME:AddMessage(tostring(temp))
db[unit].enable = temp
self:UpdateClassIcon(unit)

end

function ClassIcon:CreateIcon(unit)
	--if db[unit] and db[unit].enable then
		local frame = LUF.frames[unit];
		frame.lufclassicon = CreateFrame("frame",nil,frame)
	
		frame.lufclassicon:SetWidth(54)
		frame.lufclassicon:SetHeight(54)
		--frame.lufclassicon:SetPoint("TOPLEFT",frame,"TOPLEFT",db[unit].x,db[unit].y)
	
		frame.lufclassicon.icon = frame.lufclassicon:CreateTexture(nil,"ARTWORK")
		frame.lufclassicon.border = frame.lufclassicon:CreateTexture(nil,"OVERLAY")
	
	    frame.lufclassicon.icon:SetWidth(20)
		frame.lufclassicon.icon:SetHeight(20)
		frame.lufclassicon.icon:SetPoint("CENTER")
    
		frame.lufclassicon.border:SetTexture("Interface\\Minimap\\MiniMap-TrackingBorder")
		frame.lufclassicon.border:SetWidth(54)
		frame.lufclassicon.border:SetHeight(54)
		frame.lufclassicon.border:SetPoint("CENTER",11,-12)
		RaiseFrameLevel(frame.lufclassicon)
	--end
end

function ClassIcon:OnEnable()
	DEFAULT_CHAT_FRAME:AddMessage("ClassIcon Module is enable")
	self:UpdateClassIcon("player")
	self:UpdateClassIcon("party1")
    self:RegisterEvent("PLAYER_TARGET_CHANGED")
end

function ClassIcon:UpdateClassIcon(unit)
	local frame = LUF.frames[unit];
	
	if db[unit].enable and UnitIsPlayer(unit) then
		--DEFAULT_CHAT_FRAME:AddMessage(unit.." is ".."true")
		frame.lufclassicon.icon:SetTexture("Interface\\WorldStateFrame\\Icons-Classes");
		local _,class = UnitClass(unit);
		local Coord = CLASS_BUTTONS[class]
		frame.lufclassicon.icon:SetTexCoord(Coord[1],Coord[2],Coord[3],Coord[4])
		frame.lufclassicon:Show()
	else
		--DEFAULT_CHAT_FRAME:AddMessage(unit.." is ".."false")
		frame.lufclassicon:Hide()
	end
end

